package com.fancy.common;

import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

import com.fancy.common.constant.ResponseConstants;

/**
 * 设置返回数据结构
 * 
 */
public class DataPipe {

	/**
	 * 返回结构meta key
	 */
	private static final String CONTEXT_KEY_META = "meta";

	/**
	 * 返回结构response key
	 */
	private static final String CONTEXT_KEY_RESPONSE = "response";

	private Map<String, Object> model;

	/**
	 * 清楚model里多余的数据只保留 meta 和 response
	 * 
	 * @param map
	 */
	private DataPipe(Map<String, Object> map) {
		this.model = map;
		Object meta = map.get(CONTEXT_KEY_META);
		Object response = map.get(CONTEXT_KEY_RESPONSE);
		map.clear();
		if (meta != null) {
			model.put(CONTEXT_KEY_META, meta);
		}
		if (response != null) {
			model.put(CONTEXT_KEY_RESPONSE, response);
		}
	}

	/**
	 * 设置返回结构中的 Response结构
	 * 
	 * @param response
	 * @return
	 */
	public DataPipe response(Object response) {
		model.put(CONTEXT_KEY_RESPONSE, response);
		return this;
	}

	/**
	 * 设置返回结构中的 Meta结构
	 * 
	 * @param code
	 * @return
	 */
	public DataPipe meta(int code) {
		Meta meta = (Meta) model.get(CONTEXT_KEY_META);
		if (meta == null) {
			model.put(CONTEXT_KEY_META, new Meta(code));
		} else {
			meta.code = code;
		}
		return this;
	}

	/**
	 * 设置返回结构中的 Meta结构
	 * 
	 * @param code
	 * @return
	 */
	public DataPipe meta(long timestamp) {
		Meta meta = (Meta) model.get(CONTEXT_KEY_META);
		if (meta == null) {
			model.put(CONTEXT_KEY_META, new Meta(timestamp));
		} else {
			meta.timestamp = timestamp;
		}
		return this;
	}

	/**
	 * 设置返回结构中的 Meta结构
	 * 
	 * @param code
	 * @param message
	 * @return
	 */
	public DataPipe meta(int code, String message) {
		Meta meta = (Meta) model.get(CONTEXT_KEY_META);
		if (meta == null) {
			model.put(CONTEXT_KEY_META, new Meta(code, message));
		} else {
			meta.code = code;
			meta.message = message;
		}
		return this;
	}

	/**
	 * 设置返回结构中的 Meta结构
	 * 
	 * @param code
	 * @param message
	 * @param timestamp
	 * @return
	 */
	public DataPipe meta(int code, String message, long timestamp) {
		Meta meta = (Meta) model.get(CONTEXT_KEY_META);
		if (meta == null) {
			model.put(CONTEXT_KEY_META, new Meta(code, message));
		} else {
			meta.code = code;
			meta.message = message;
			meta.timestamp = timestamp;
		}
		return this;
	}

	public static DataPipe in(Model model) {
		return new DataPipe(model.asMap());
	}

	public static DataPipe in(ModelAndView model) {
		return new DataPipe(model.getModel());
	}

	public static DataPipe in(Map<String, Object> map) {
		return new DataPipe(map);
	}

	/**
	 * 元数据
	 * 
	 * @author xinfeng.hu 2013-3-13下午9:50:58
	 * @version 1.0.0
	 * @category 杭州广桥网络技术有限公司(商业先知)
	 */
	class Meta {

		/**
		 * 请求处理结果状态码
		 */
		private int code;

		/**
		 * 如果code!=200的情况下，返回具体的错误信息
		 */
		private String message;

		/**
		 * 时间戳
		 */
		private Long timestamp;

		public Meta(int code) {
			this.code = code;
		}

		public Meta(long timestamp) {
			this.timestamp = timestamp;
		}

		public Meta(int code, String message) {
			this.code = code;
			this.message = message;
		}

		public Meta(int code, String message, long timestamp) {
			this.code = code;
			this.message = message;
			this.timestamp = timestamp;
		}

		public int getCode() {
			return (code == 0 ? ResponseConstants.SC_OK : code);
		}

		public void setCode(int code) {
			this.code = code;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public Long getTimestamp() {
			return timestamp;
		}

		public void setTimestamp(Long timestamp) {
			this.timestamp = timestamp;
		}
	}

}